<?php
/**
 * cookie库
 * @copyright	http://www.easymvc.com
 * @author 		光哥 <439884988@qq.com>
 * @version		1.0 - 2012-07-02
 * @package		Lib
 */
if(!defined('EASYMVC')) {
	exit('Access Denied');
}
/**
 * cookie
 */
if(!defined('EASYMVC')) {
	exit('Access Denied');
}
class Lib_Cookie {
	
	public $expire;
	public $path;
	public $domain;
	public $secure;
	public $httponly;
	
	/**
	 * 初始化
	 * @param integer $expire
	 * @param string $path
	 * @param string $domain
	 * @param boolean $secure
	 * @param boolean $httponly
	 * @return void
	 */
	public function __construct($expire=NULL,$path='/',$domain=NULL,$secure=NULL,$httponly=NULL){
		$this->expire = $expire;
		$this->path= $path;
		if(!$domain)$this->domain = Frame_Config::get('config.domain');
		$this->secure = $secure;
		$this->httponly = $httponly;
	}
	
	/**
	 * 实例化对象
	 * 
	 * @return Lib_Cookie
	 */
	public static function getInstance(){
		static  $obj;
		if(!$obj) $obj = new self();
		return $obj;
	}
	
	/**
	 * set 变量
	 * @param string $name
	 * @param string|int | array  $value
	 * @return boolean
	 */
	public function set($name,$value,$expire=3600){
		$_COOKIE[$name] = $value;
		$this->expire = time()+$expire;
		return setcookie($name,$value,$this->expire,$this->path,$this->domain,$this->secure,$this->httponly);
	}
	
	/**
	 * get 变量
	 * @param string $name
	 * @return bool
	 */
	public function get($name){
		if($this->is_set($name)) {
			return $_COOKIE[$name];
		}
		return false;
	}
	/**
	 * 删除cookie
	 * @param string $name
	 */
	public function del($name){
		unset($_COOKIE[$name]);
		//print_r($_COOKIE);
		return $this->set($name, NULL, -1);
	}
	/**
	 * 判断name是否存在
	 * @param string $name
	 */
	public function is_set($name){
		if(isset($_COOKIE[$name])){
			return true;
		}
		return false;
	}
	
	/**
	 * 清空cookie
	 */
	public function destory(){
		$cookie = $_COOKIE;
		if($cookie){
			foreach($cookie as $name=>$value){
				$this->del($name);
			}
			
		}
	}
	
}
?>